home *** CD-ROM | disk | FTP | other *** search
/ Amoszine 11 / Amoszine 11 (Disk 2 of 2).adf / Ben_Wyatt_Source.lha / Fast_Sort.AMOS / Fast_Sort.amosSourceCode
AMOS Source Code  |  2004-04-12  |  1KB  |  50 lines

  1. ' Fast sort procedure
  2. ' ~~~~~~~~~~~~~~~~~~~
  3. ' by Ben Wyatt, bwyatt@paston.co.uk
  4. '
  5. ' Sorts a list of numbers that have a known maximum value
  6. ' As it's name suggests, it's very fast - faster even than the Sort command! 
  7. ' It was also invented by me 8-) 
  8.  
  9. ' Reserve room for the massive list of numbers to sort 
  10. Set Buffer 200
  11.  
  12. NUMBERS=50000 : MXVALUE=200
  13.  
  14. Dim LIST(NUMBERS)
  15. Global LIST()
  16.  
  17. Print "Making up some random numbers"
  18. For N=0 To NUMBERS
  19.    LIST(N)=Rnd(MXVALUE)
  20. Next N
  21.  
  22. Print "Now sorting"+Str$(NUMBERS)+" numbers"
  23. Timer=0
  24. _FSORT[MXVALUE,0,NUMBERS]
  25. Print "Sorted in"+Str$(Timer/50.0)+" seconds"
  26.  
  27. Procedure _FSORT[MXVALUE,STNUM,ENNUM]
  28.  
  29.    ' Sorts array LIST(), that contains the maximum value of MXVALUE 
  30.    ' between element STNUM and ENNUM
  31.  
  32.    ' Reserve some memory for the number of each number :-?
  33.    Dim CNT(MXVALUE)
  34.  
  35.    ' Count the occurances of each number
  36.    For N=STNUM To ENNUM
  37.       Inc CNT(LIST(N))
  38.    Next N
  39.  
  40.    ' Put the numbers back into the array, but sorted
  41.    ELEMENT=STNUM
  42.    For N=0 To MXVALUE
  43.       If CNT(N)>0
  44.          For M=1 To CNT(N)
  45.             LIST(ELEMENT)=N : Inc ELEMENT
  46.          Next M
  47.       End If 
  48.    Next N
  49.  
  50. End Proc